var hideSuccessMessageInterval;

$(document).ready(function () {
  $('input[readonly=readonly], textarea[readonly=readonly]').css('background-color', '#dddddd');
  
  $('#fetch_movie_info').click(function(){
    var movie_title = $('#frm_movie_title').val();
    if (movie_title != '') {
      $('#ajax-loader').show();
      $('#fetch_movie_info').attr('disabled', 'disabled');
      $('#frm_movie_title').attr('disabled', 'disabled');
      $('#frm_movie_trailer_url').attr('disabled', 'disabled');
      $('#smt').attr('disabled', 'disabled');
      $.ajax({
        url: $('#frontcontroller').val() + '/offers/fetch-movie-info-ajax?title=' + movie_title,
        success: function(data) {
          $('#ajax-loader').hide();
          $('#fetch_movie_info').removeAttr('disabled');
          $('#frm_movie_title').removeAttr('disabled');
          $('#frm_movie_trailer_url').removeAttr('disabled');
          $('#smt').removeAttr('disabled');

          if (data == 'NOT_OK') {
            alert('Unable to fetch movie information.\n\nPlease try again in few seconds.')
            return false;
          }

          if (data == 'NOT_FOUND') {
            alert("Movie could not be found!\n\nPlease retry with a different Movie Title.");
          } else {
            data = jQuery.parseJSON(data);
            if (data.title.toLowerCase() != $('#frm_movie_title').val().toLowerCase() && !confirm("Movie title found \"" + data.title + "\".\n\nDo you wish to approve this?")) {
              return false;
            }
            if (!data.trailer) {
              alert("Unable to find the movie trailer URL.\n\nPlease input Trailer URL manually.");
              $('#frm_movie_trailer_url').focus();
            }
            $('#frm_movie_title').val(data.title);
            $('#frm_movie_trailer_url').val(data.trailer);
            $('#frm_is_movie_nonempty').val(1);
          }
        }
      });
    }
  });

  if ($('#offer_is_copy').val() == '1') {
    $('#fetch_movie_info').trigger('click');
  }

  var url = new String(document.location.href);
  var anchor_pos = url.indexOf('#');
  if (anchor_pos != -1) {
    if ($('.' + url.substr(anchor_pos+1)).attr('class')) {
      scrollPage('.' + url.substr(anchor_pos+1));
    }
  }

  if ($('.succDis').attr('class')) {
    hideSuccessMessageInterval = setInterval('hideSuccessMessage()', 4000);
  }

  $('.frmRow req').css('color', '#cc0000').html('*');



  if ($('#frm_session_time_d').attr('readonly') != 'readonly') {
    $('#frm_session_time_d').DatePicker({
      date: $('#frm_session_time_d').val(),
      current: $('#frm_session_time_d').val(),
      format:'Y-m-d',
      starts: 1,
      onChange: function(formated, dates){
        $('#frm_session_time_d').val(formated);
        $('#frm_session_time_d').DatePickerHide();
      }
    });
  }
  if ($('#frm_offer_from_d').attr('readonly') != 'readonly') {
    $('#frm_offer_from_d').DatePicker({
      date: $('#frm_offer_from_d').val(),
      current: $('#frm_offer_from_d').val(),
      format:'Y-m-d',
      starts: 1,
      onChange: function(formated, dates){
        $('#frm_offer_from_d').val(formated);
        $('#frm_offer_from_d').DatePickerHide();
      }
    });
  }
  if ($('#frm_offer_to_d').attr('readonly') != 'readonly') {
    $('#frm_offer_to_d').DatePicker({
      date: $('#frm_offer_to_d').val(),
      current: $('#frm_offer_to_d').val(),
      format:'Y-m-d',
      starts: 1,
      onChange: function(formated, dates){
        $('#frm_offer_to_d').val(formated);
        $('#frm_offer_to_d').DatePickerHide();
      }
    });
  }

  $('#frm_session_time_d').blur(function() {
    setTimeout(function() {
      $('#frm_offer_to_d').val($('#frm_session_time_d').val());
    }, 500);
  });
  $('#frm_session_time_t_hour').change(function() {
    $('#frm_offer_to_t_hour').val($('#frm_session_time_t_hour').val());
  });
  $('#frm_session_time_t_minute').change(function() {
    $('#frm_offer_to_t_minute').val($('#frm_session_time_t_minute').val());
  });
  
  $('#frm_cinema').change(function () {
    $.ajax({
      url : $('#frontcontroller').val() + '/offers/fetch-cinema-info-ajax?id='+$(this).val(),
      success: function (data) {
        var cinfo = data.split('|||||');
        $('#frm_screen_no').html(cinfo[0]);
        $('#frm_offer_from_t_hour').val(cinfo[1]);
        $('#frm_offer_from_t_minute').val(cinfo[2]);
        $('#frm_offer_from_d').val(cinfo[3]);
      },
      error: function () {
        $('#frm_screen_no').html('<option value="">-- select a cinema --</option>');
      }
    });
  });
  
});

function scrollPage(x) {
  $.scrollTo(x, 500);
}

function hideSuccessMessage() {
  $('.succDis').fadeOut(2000, function() {
    scrollPage($('.l'));
    clearInverval(hideSuccessMessageInterval);
  });
}

function popupWindow(url, w, h) {
  window.open(url, 'name' , 'height=' + h + ',width=' + w);
}